package com.example.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.entity.Message;
import com.example.entity.MessageVO;
import com.example.service.MessageService;

@RequestMapping("/message")
@RestController
public class MessageController {
	
	@Autowired
	private MessageService messageService;
	
	/**
	 * 多条件查询消息
	 * @param messageVO
	 * @return
	 */
	@RequestMapping("/findMessage")
	public Map<String, Object> findMessage(MessageVO messageVO){
		Map<String, Object> reMap=new HashMap<String, Object>();
		messageVO.setOffset((messageVO.getPage()-1)*messageVO.getLimit());
		List<Message> list=messageService.findMessage(messageVO);
		int count=messageService.getCount(messageVO);
		reMap.put("code","0");
		reMap.put("msg", "success");
		reMap.put("count", count);
		reMap.put("data", list);
		return reMap;
	}
	
	/**
	 * 发消息
	 * @param message
	 * @return
	 */
	@RequestMapping(path = "/sendNews")
	public Map<String, Object> sendNews(Message message) { // 受影响的行数 
		int rows = messageService.sendNews(message);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "发送成功" : "发送失败");
		return rsMap;

	}
	
	/**
	 * 查看消息是否查看
	 * @param message
	 * @return
	 */
	@PostMapping("/selectAllMessageIsLook")
	public Map<String, Object> selectAllMessageIsLook(Message message) {
		// 调用查询
		List<Message> list = messageService.selectMyMessageIsLook(message);
		// 返回结果
		Map<String, Object> rslMap = new HashMap<String, Object>();
		if (list.size()>=1) {
			rslMap.put("msg", "xiaoxi");
		}else {
			rslMap.put("msg", "");
		}
		return rslMap;
	}
	
	
	/**
	 * 全部标为已读
	 * @param message
	 * @return
	 */
	@PostMapping(path = "/updateMessageIsLook")
	public Map<String, Object> updateMessageIsLook(Message message) {
		// 受影响的行数
		int rows = messageService.updateMessageIsLook(message);
		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "已全部标为已读" : "目前没有可读消息");
		return rsMap;

	}

}